package uk.org.scuts.bantstadium2;

import java.util.Random;

import uk.org.scuts.bantstadium2.attacks.Attack;
import android.widget.ProgressBar;
import android.widget.TextView;

class Battle {
	
	// Constructors
	public Battle(ActivityQuickBattle ctx,
			      String player,
			      String opponent) {
		_ctx = ctx;
		_log = "";
		String displayName;
		_am = new AttackManager();
		_cm = new CharManager(ctx.charCfg);
		
		displayName = opponent;
		
		if (player.equals(opponent))
		{
			displayName = "Evil " + opponent;
		}
		
		_player = CreateCharacter(player,player,true);
		_opponent = CreateCharacter(opponent,displayName,false);
		
		_player.setOpponent(displayName);
		_opponent.setOpponent(player);
	}
	
	// Methods
	void RunRound()
	{	
		Random rand = new Random();
		double playerSpeed = (rand.nextInt(25) + 75) * _player._speed;
		double opponentSpeed = (rand.nextInt(25) + 75) * _opponent._speed;
		Boolean playerFirst = (playerSpeed >= opponentSpeed);
		
		Character first;
		Character second;
		
		if (playerFirst) {
			first = _player;
			second = _opponent;
		} else {
			first = _opponent;
			second = _player;
		}
		
		Attack firstStrike = first.GetAction();
		Attack secondStrike = second.GetAction();	
		
		firstStrike.Setup(first, second);
		secondStrike.Setup(second, first);
		
		_logColour = 0xFF000000;
		
		_log = firstStrike.Execute(first, second);
		if ( CheckAlive() ) {
			_log += "\n";
			_log += secondStrike.Execute(second, first);
			if ( CheckAlive() ) {
				firstStrike.Restore(first, second);
				secondStrike.Restore(second, first);
			}
		}
		
		UpdateViews();
	}
	
	Boolean CheckAlive() {
		Boolean ret = false;
		if ( !_player.IsAlive() && !_opponent.IsAlive() )
		{
			_logColour = 0xFF000000;
			_log += ("\n" + _player.Name() + " and " + _opponent.Name()
					 + " simultaneously succumb to their wounds.\n\nDRAW!");
		}
		else if ( !_opponent.IsAlive() )
		{
			_logColour = 0xFF008800;
			_log += ("\n" + _opponent.Name() + " was defeated.\n\n" + _player.Name() + " wins!");
		}
		else if ( !_player.IsAlive() )
		{
			_logColour = 0xFFFF0000;
			_log += ("\n" + _player.Name() + " was defeated.\n\n" + _opponent.Name() + " wins!");
		}
		else {
			ret = true;
		}
		return ret;
	}
	
	private void UpdateViews()
	{
		TextView log = (TextView)_ctx.findViewById(R.id.battleLog);
        log.setText(_log);
        log.setTextColor(_logColour);
        _log = "";
        TextView playerHp = (TextView)_ctx.findViewById(R.id.playerHp);
        playerHp.setText(_player.HP());
        TextView opponentHp = (TextView)_ctx.findViewById(R.id.opponentHp);
        opponentHp.setText(_opponent.HP());
        ProgressBar playerBar = (ProgressBar)_ctx.findViewById(R.id.playerBar);
        playerBar.setProgress(_player.PercentHP());
        ProgressBar opponentBar = (ProgressBar)_ctx.findViewById(R.id.opponentBar);
        opponentBar.setProgress(_opponent.PercentHP());
        TextView playerName = (TextView)_ctx.findViewById(R.id.playerName);
        playerName.setTextColor(_player.NameColour());
        TextView opponentName = (TextView)_ctx.findViewById(R.id.opponentName);
        opponentName.setTextColor(_opponent.NameColour());
	}
	
	Character CreateCharacter(String name, String displayName, Boolean user) {
		CharBase b = _cm.at(name);

		return new Character(user,
							displayName,
		            		b._gender,
		            		Double.parseDouble(b._attack),
		            		Double.parseDouble(b._defense),
		            		Double.parseDouble(b._cunning),
		            		Integer.parseInt(b._hp),
		            		Double.parseDouble(b._speed),
		            		_am.at(b._attack0),
		            		_am.at(b._attack1),
		            		_am.at(b._attack2),
		            		_am.at(b._attack3));
	}
	
	Character Player() { return _player; }
	Character Opponent() { return _opponent; }
	String Log() { return _log; }

	// Members
	private Character _player;
	private Character _opponent;
	private ActivityQuickBattle _ctx;
	private String _log;
	private int _logColour;
	private AttackManager _am;
	private CharManager _cm;
	
}
